package com.hdax.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CusIntention;
import com.hdax.entity.CusOpportunity;
import com.hdax.entity.Customer;
import com.hdax.service.CusIntentionService;
import com.hdax.service.CusOpportunityService;
import com.hdax.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.Oneway;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CusIntentionService cusIntentionService;
    @Autowired
    private CusOpportunityService cusOpportunityService;

    @GetMapping("/list")
    public IPage<Customer> list(Integer page, Integer limit) {
        IPage<Customer> iPage = new Page(page, limit);
        return customerService.findCustomer(iPage, new HashMap());
    }

    @PostMapping("/condition")
    public IPage<Customer> conditional(@RequestBody Map map) {
        IPage<Customer> iPage = new Page<>(Long.parseLong(map.get("page").toString()), Long.parseLong(map.get("limit").toString()));
        return customerService.findCustomer(iPage, map);
    }

    @PostMapping("/updateCustomerState")
    public String updateCustomerState(Customer customer, @RequestParam("intentionState") Integer IntentionState) {
        customer.setUpdateTime(LocalDateTime.now());
        CusIntention cusIntention = cusIntentionService.getOne(new QueryWrapper<CusIntention>().eq("cus_id", customer.getId()));
        cusIntention.setIntentionState(IntentionState);
        cusIntentionService.updateById(cusIntention);
        customerService.updateById(customer);
        return "success";
    }

    @PostMapping("/findCustomerByState")
    public List<Customer> findCustomerByState(Integer state) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_state", state);
        return customerService.list(queryWrapper);
    }

    @PostMapping("/updateCustomerStateAndOpp")
    public String updateCustomerStateAndOpp(Customer customer, @RequestParam("oppState") Integer oppState) {
        customer.setUpdateTime(LocalDateTime.now());
        CusOpportunity cusOpportunity = cusOpportunityService.getOne(new QueryWrapper<CusOpportunity>().eq("cus_id", customer.getId()));
        cusOpportunity.setOpportunityState(oppState);
        cusOpportunityService.updateById(cusOpportunity);
        customerService.updateById(customer);
        return "success";
    }

    @PostMapping("/findCustomerPhoneById")
    public String findCustomerPhoneById(Integer id){
        Customer customer = customerService.findCustomerById(id);
        return customer.getPhone();
    }

    @PostMapping("/findCustomerById")
    public List<Customer> findCustomerById(Integer id){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return customerService.list(queryWrapper);
    }

    @GetMapping("/toCustomer")
    public ModelAndView toCustomer(){
        return new ModelAndView("customer/customer");
    }

    @GetMapping("/toCustomerPage")
    public ModelAndView toCustomerPage(Integer id){
        ModelAndView modelAndView = new ModelAndView("customer/customer_page");
        modelAndView.addObject("customer",customerService.findCustomerById(id));
        return modelAndView;
    }

    @PostMapping("/updateCustomer")
    public String updateCustomer(Customer customer){
        System.out.println(customer);
        customerService.updateById(customer);
        return "success";
    }

    @PostMapping("/findCustomerCount")
    public Map findCustomerCount(){
        Map map = new HashMap();
        map.put("month",customerService.findThisMonthCustomerAddCount());
        map.put("all",customerService.findCustomerCount());
        return map;
    }

    @PostMapping("/findCustomerChainCompare")
    public Float findCustomerChainCompare(){
        return customerService.findCustomerChainCompare();
    }


    @PostMapping("/findThisYearCustomerIncre")
    public List findThisYearCustomerIncre(){
        Map thisYearCustomerIncre = customerService.findThisYearCustomerIncre();
        System.out.println(thisYearCustomerIncre);
        ArrayList arrayList = new ArrayList<>(thisYearCustomerIncre.values());
        System.out.println(arrayList);
        return arrayList;
    }

}

